package org.blueoxygen.rest.geneology.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.blueoxygen.cimande.CimandeAction;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.blueoxygen.rest.geneology.Person;
import org.blueoxygen.rest.geneology.RelationshipStatus;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

@Action(value = "childs", results = {
// @Result(name = CimandeAction.SHOW, type = "velocity", location =
// "/person/child-list.vm"),
// @Result(name = CimandeAction.SUCCESS, type = "velocity", location =
// "/person/child-add-success.vm"),
@Result(name = CimandeAction.DELETE, type = "redirect", location = "/person/${id}/"),
// @Result(name = "addnew", type = "velocity", location =
// "/person/child-add-new.vm")
})
public class ChildsController extends CimandeAction implements
		ModelDriven<Object> {
	private Person person = new Person();
	private List<Person> persons = new ArrayList<Person>();

	public String create() {
		Person parent = (Person) manager.getById(Person.class, request
				.getParameter("id"));
		Person child = new Person();
		child.setId(null);
		child.setFirstName(person.getFirstName());
		child.setLastName(person.getLastName());
		// child.setGender(person.getGender());
		int rstatus = new Integer(request.getParameter("rstatus"));
		child.setStatus(RelationshipStatus.values()[rstatus]);
		child.setAddress(person.getAddress());
		// child.setParent(parent);

		manager.save(child);

		// parent.getChild().add(child);

		manager.save(parent);

		person = parent;

		return "person/child-add-success";
	}

	public String show() {
		person = (Person) manager.getById(Person.class, person.getId());
		String hql = "FROM " + Person.class.getName()
				+ " WHERE parent_id IS NULL AND id != '" + person.getId() + "'";

		// if (person.getParent() != null) {
		// hql += " AND id != '" + person.getParent().getId() + "'";
		// }

		persons = session.createQuery(hql).list();

		return "person/child-list";
	}

	public String addnew() {
		// System.out.println(person.getId());
		// person = new Person();

		return "person/child-add-new";
	}

	public String update() {
		person = (Person) manager.getById(Person.class, person.getId());
		Person child = (Person) manager.getById(Person.class, request
				.getParameter("cid"));

		// child.setParentId(person.getId());
		// person.getChild().add(child);

		// manager.save(child);
		manager.save(person);

		return "person/child-add-success";
	}

	public String remove() {
		person = (Person) manager.getById(Person.class, person.getId());
		Person child = (Person) manager.getById(Person.class, request
				.getParameter("cid"));
		// person.getChild().remove(child);
		// child.setParent(null);

		// manager.save(child);
		manager.save(person);

		return DELETE;
	}

	public String getId() {
		return id;
	}

	public Object getModel() {
		return persons.size() > 0 ? persons : person;
	}
}
